﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.OleDb;
using Home.Utils;
using NhanAi.Entity;

namespace NhanAi.DAO
{
    public class TaxonomyRelationshipDao:DBConnection
    {
        public void Create(TaxonomyRelationship r)
        {
            NonQueryCommandBuilder nb = new NonQueryCommandBuilder("HmTaxonomyRelationship");
            if (Connect())
            {
                nb.AddValueField("TaxonomyId", r.TaxonomyId);
                nb.AddValueField("ObjectId", r.ObjectId);
                nb.AddValueField("TermOrder", r.TermOrder);              

                OleDbCommand cmd = nb.GetCreateCommand(conn);
                cmd.ExecuteNonQuery();
              
                Disconnect();
            }

        }
        public void Create(int objectId,int taxonomyId)
        {
            NonQueryCommandBuilder nb = new NonQueryCommandBuilder("HmTaxonomyRelationship");
            if (Connect())
            {
                nb.AddValueField("TaxonomyId", taxonomyId);
                nb.AddValueField("ObjectId", objectId);
                nb.AddValueField("TermOrder", 0);
                OleDbCommand cmd = nb.GetCreateCommand(conn);
                cmd.ExecuteNonQuery();

                Disconnect();
            }

        }
        public void Update(User u)
        {
            //NonQueryCommandBuilder nb = new NonQueryCommandBuilder("HmUser");
            //if (Connect())
            //{
               
            //    nb.AddValueField("ID", u.Id);
            //    OleDbCommand cmd = nb.GetUpdateCommand(conn, true);
            //    Disconnect();
            //}
            throw new NotImplementedException("Method is not implemented");

        }
        public void Delete(int taxId,int objId)
        {
            NonQueryCommandBuilder nb = new NonQueryCommandBuilder("HmTaxonomyRelationship");
            if (Connect())
            {

                nb.AddConditionField("TaxonomyId", taxId);
                nb.AddConditionField("ObjectId", objId);
                OleDbCommand cmd = nb.GetDeleteCommand(conn, true);
                Disconnect();
            }

        }
        public void Delete(int postId)
        {
            NonQueryCommandBuilder nb = new NonQueryCommandBuilder("HmTaxonomyRelationship");
            if (Connect())
            {               
                nb.AddConditionField("ObjectId", postId);
                OleDbCommand cmd = nb.GetDeleteCommand(conn, true);
                cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        public TaxonomyRelationship Read(int taxId,int objectId)
        {
            TaxonomyRelationship u = null;
            if (Connect())
            {
                OleDbCommand cmd = new OleDbCommand("select * from HmTaxonomyRelationship where taxonomyId=@tid and ObjectId=@id", conn);
                cmd.Parameters.AddWithValue("@tid", taxId);
                cmd.Parameters.AddWithValue("@id", objectId);
                OleDbDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    u = new TaxonomyRelationship();
                    u.TaxonomyId = dr.GetInt32(0);
                    u.ObjectId = dr.GetInt32(1);
                    u.TermOrder = dr.GetInt32(2);                 

                }
                Disconnect();
            }
            return u;
        }
        public List<TaxonomyRelationship> ReadMany(int objectId)
        {
            List<TaxonomyRelationship> list = new List<TaxonomyRelationship>();
            OleDbCommand cmd = new OleDbCommand("select * from HmTaxonomyRelationship where ObjectId=@id", conn);
            cmd.Parameters.AddWithValue("@id",objectId);
            if (Connect())
            {              

                OleDbDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    TaxonomyRelationship u = new TaxonomyRelationship();
                    u.TaxonomyId = dr.GetInt32(0);
                    u.ObjectId = dr.GetInt32(1);
                    u.TermOrder = dr.GetInt32(2);

                    list.Add(u);
                }
            }
            return list;
        }
    }
}